import DisconnectEnable from "../event/DisconnectEnable";

const _FRAME_TIME = 1 / 30;
/** 帧管任务管理器 */
class FrameManagerImpl {
    private isRunning = false
    private connector = new DisconnectEnable<number>();
    /** 绑定帧任务 */
    connect(call: (key: number) => void) {
        return this.connector.bind(call)
    }
    run() {
        if (this.isRunning) return
        this.isRunning = true;
        // let last = DateTime.now();
        // let now = DateTime.now();
        // let dual = 0
        let h = 0
        game.GetService('RunService').PreAnimation.Connect((t) => {
            // now = DateTime.now();
            // dual = now.UnixTimestampMillis - last.UnixTimestampMillis
            // print(this.connector.size())
            if (h === 0) {
                h = 1
            } else {
                h = 0
            }
            this.connector.fire(h)
            // last = now
        })
        // task.spawn(() => {
        //     while (true) {
        //         task.wait(_FRAME_TIME)
        //         task.spawn(() => {
        //             now = DateTime.now();
        //             dual = now.UnixTimestampMillis - last.UnixTimestampMillis
        //             print(dual)
        //             this.connector.fire(dual)
        //             last = now
        //         })
        //     }
        // })
    }
}
const FrameManager = new FrameManagerImpl();
FrameManager.run()
export default FrameManager;